package com.study.d4_sort;

public class BinSearch {
    public static void main(String[] args) {
        int[] arr={5,10,11,16,22,155,200,517};
        System.out.println(search(arr,16));
        System.out.println(search(arr,170));
        System.out.println(search(arr,155));

    }
 static int search(int[] arr,int target){
        int left = 0;
        int right = arr.length;
        while(left<=right){
            int mid = (left+right)/2;
            if(arr[mid]>target){
                right=mid-1;
            }else if(arr[mid]<target){
                left=mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
